package workexp.alg.question.tree;

import org.junit.Test;
import workexp.alg.env.TreeNode;

public class IsValidBST {
    TreeNode pre;
    public boolean isValidBST(TreeNode root) {
        if(root==null){
            return true;
        }
        if(!isValidBST(root.left)){
            return false;
        }
        if(pre!=null&&pre.val>=root.val){
            return false;
        }
        pre=root;

        if(!isValidBST(root.right)){
            return false;
        }
        return true;

    }

    @Test
    public  void test(){
        Integer[] nums = {2,1,3};
        TreeNode root = TreeNode.createTree(nums);
        System.out.println(root);
        TreeNode.show(root);
        boolean result = isValidBST(root);
        System.out.println(result);
    }

}
